# import wordcloud
# # 创建词云对象
# w = wordcloud.WordCloud(font_path='AdobeHeitiStd-Regular.otf',
#      max_words=500, max_font_size=40, background_color='white')
# # 加载词云图用到的文本
# file = open(r'xiyouji.txt', encoding='utf-8')
# string = file.read()
# file.close()
# w.generate(string)
# # 生成词云图
# w.to_file('xiyou.jpg')


import wordcloud
import numpy as np
from PIL import Image
picture = Image.open("wukong.png")  # 加载图片文件，返回一个图片对象
mk = np.array(picture)                 # 将图片对象转换成数组
# 创建词云对象
w = wordcloud.WordCloud(font_path='AdobeHeitiStd-Regular.otf', mask=mk,
                             max_words=500, background_color='white')
file = open(r'xiyouji.txt', encoding='utf-8')
string = file.read()
file.close()
# 加载词云图用到的文本
w.generate(string)
# 生成词云图
w.to_file('xiyou.jpg')

